package hudson.msservice.message_entry;

import hudson.Launcher;
import hudson.logging.HudsonTestLog;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.Result;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import hudson.msservice.messageservice.MessageServiceProxy;
import hudson.msservice.Messages;

public class RtxMessageEntry {

    private String Rtx_title = "";
    private String Rtx_content = "";

    public String getRtx_content() {
        return this.Rtx_content;
    }

    public void setRtx_content(String Rtx_content) {
        this.Rtx_content = Rtx_content;
    }

    public String getRtx_title() {
        return this.Rtx_title;
    }

    public void setRtx_title(String Rtx_title) {
        this.Rtx_title = Rtx_title;
    }

    public RtxMessageEntry() {
    }

    public RtxMessageEntry(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
        this.Rtx_title = (build.getFullDisplayName() + Messages.Rtx_Title());
        Date dt = build.getTimestamp().getTime();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tm = fm.format(dt);
        String star = "";
        Logger.getLogger(RtxMessageEntry.class.getName()).log(HudsonTestLog.level, " the startor 01 "+star);
       
        Logger.getLogger(RtxMessageEntry.class.getName()).log(HudsonTestLog.level, " the startor 03 "+star);
        

        this.Rtx_content = (Messages.Rtx_Startor() + star + "\n" + Messages.Rtx_StartTime() + tm + "\n" + Messages.Rtx_Duration() + build.getDurationUntilNow() + "\n" + Messages.Rtx_BuildResult() + MessageUtil.getChineseOfResult(build.getResult().toString()) + "\n" + Messages.Rtx_BuildLog() + "[" + Messages.Rtx_ConsoleOutput() + "|" + build.getConsoleTextUrlOfCurrentBuild() + "]");
    }

    public void sendRtxMessage(String receive, AbstractBuild<?, ?> build) {
        String sender = "ptadmin";
        String receiver = "";
        if (receive.trim().length() == 0) {
//            receive = MysqlDb.getMessageReceiverList(build.getJobName(), build.getNumber());
        }
        for (String str : receive.split(";")) {
            if (str.trim().length() != 0 && MessageUtil.isTencentDomainUser(str)) {
                receiver = receiver + str.trim() + ";";
            }
        }
        String title = this.Rtx_title;
        String msgInfo = this.Rtx_content;
        String priority = "Low";
        MessageServiceProxy instance = MessageServiceProxy.getInstance();
        instance.sendRTX(sender, receiver, title, msgInfo, priority);
    }
    
    /**
     * when tools's upstream project build failture ,abort or others 
     * notify the user that could not schedule its' tools
     */
     public void sendRtxMessageNoticeUpstreamNotSuccess(String receive, AbstractBuild<?, ?> build) {
        String sender = "ptadmin";
        String receiver = "";
        if (receive.trim().length() == 0) {
//            receive = MysqlDb.getMessageReceiverList(build.getJobName(), build.getNumber());
        }
        for (String str : receive.split(";")) {
            if (str.trim().length() != 0 && MessageUtil.isTencentDomainUser(str)) {
                receiver = receiver + str.trim() + ";";
            }
        }
        String title = build.getFullDisplayName()+" tools schedule error";
        String msgInfo = build.getFullDisplayName()+" build not success ,it's tools not schedule";
        String priority = "Low";
        MessageServiceProxy instance = MessageServiceProxy.getInstance();
        instance.sendRTX(sender, receiver, title, msgInfo, priority);
    }
    
}